package recipese.bean;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.apache.commons.collections.functors.InstantiateFactory;
import org.apache.commons.collections.list.LazyList;
import org.datanucleus.jpa.annotations.Extension;

import com.google.appengine.api.datastore.Text;

@Entity
public class Recipe implements JpaBean {

	private static final long serialVersionUID = -5216052819681963967L;

	@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String id;
    
    @Basic(optional = false)
    private String name;
	
    @Basic(optional = false)
    private Text instructionsText;
    
    @Transient
    private List<Ingredient> ingredients = new ArrayList<Ingredient>();
    
    @Transient
    private List<ShareRecipe> shares = new ArrayList<ShareRecipe>();
    
    @Basic
    private String userId;
    
    @Basic
    private String url;

    @SuppressWarnings("unchecked")
	public Recipe() {
    	this.ingredients = LazyList.decorate(new ArrayList<Ingredient>(), new InstantiateFactory(Ingredient.class));
    	this.shares = LazyList.decorate(new ArrayList<ShareRecipe>(), new InstantiateFactory(ShareRecipe.class));
    }
    
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getInstructions() {
		return (instructionsText != null) ? instructionsText.getValue() : null;
	}

	public void setInstructions(String instructions) {
		this.instructionsText = new Text(instructions);
	}

	public List<Ingredient> getIngredients() {
		return ingredients;
	}

	public void setIngredients(List<Ingredient> ingredients) {
		this.ingredients = ingredients;
	}

	public Text getInstructionsText() {
		return instructionsText;
	}

	public void setInstructionsText(Text instructionsText) {
		this.instructionsText = instructionsText;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public List<ShareRecipe> getShares() {
		return shares;
	}

	public void setShares(List<ShareRecipe> shares) {
		this.shares = shares;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
